package sketch.main;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedList;
import java.util.List;

import junit.framework.TestResult;
import junit.textui.TestRunner;

import randoop.util.Command;
import sketch.util.Globals;

public class CodeToolkit {
	
	public static void compile(File f) {
		List<File> files = new LinkedList<File>();
		files.add(f);
		compile(files);
	}
	
	public static void compile(List<File> sources) {
		compile(sources, Globals.getClasspath(), "Compiling " + sources.size() + " files...");
	}
	
	public static void compileToDir(List<File> sources, String dir) {
		compile(sources, Globals.getClasspath(), dir, "Compiling " + sources.size() + " files...");
	}
	
	public static void compile(List<File> sources, String classPath) {
		if(classPath == null) {
			compile(sources);
		} else {
		    compile(sources, Globals.getClasspath() + Globals.pathSep  + classPath, "Compiling...");
		}
	}
	
	public static void compile(List<File> sources, String classPath, String nonVerboseMessage) {
		//Command.exec(cmd)
		List<String> command = new LinkedList<String>();
		command.add("javac");
		command.add("-g");
		command.add("-cp");
		command.add(classPath);
		
		for(File f : sources) {
			command.add(f.getAbsolutePath());
		}
		
	    Command.runCommand(command.toArray(new String[0]), "JAVAC>", true,
	    		nonVerboseMessage, false);
	}
	
	public static void compile(List<File> sources, String classPath, String outputDir, String nonVerboseMessage) {
		//Command.exec(cmd)
		List<String> command = new LinkedList<String>();
		command.add("javac");
		command.add("-d");
		command.add(outputDir);
		command.add("-g");
		command.add("-cp");
		command.add(classPath);
		
		for(File f : sources) {
			command.add(f.getAbsolutePath());
		}
		
	    Command.runCommand(command.toArray(new String[0]), "JAVAC>", true,
	    		nonVerboseMessage, false);
	}
	
	public static Class<?> loadClassFromDir(String dir, String className) {
		File file = new File(dir);
		try {
			URL url = file.toURL();
			URL[] urls = new URL[]{url};
			ClassLoader loader = new URLClassLoader(urls);
			//System.out.println("loading from: " + dir + ", class Name: " + className);
			Class<?> clazz = loader.loadClass(className);
			return clazz;
	    } catch (Exception e) {
	    	System.err.println("Can not load class: " + className + " from dir: " + dir);
	    	throw new RuntimeException("Can not load class: " + className + " from dir: " + dir);
	    }
	}
	
	public static void execute(String classPath, String mainDriver) {
		
	}
	
	public static void main(String[] args) {
		List<File> sources = new LinkedList<File>();
		sources.add(new File("./output/MainExpansionTest.java"));
		CodeToolkit.compile(sources, Globals.getClasspath(), "Compiling a program to see where is its output.");
		
		Class<?> clazz = loadClassFromDir("./output", "MainExpansionTest");
		
		System.out.println(clazz);
		
		junit.textui.TestRunner.run(clazz);
	}
	
	public static void runUniTests(String[] tests) {
		TestRunner aTestRunner= new TestRunner();
		try {
			TestResult r= aTestRunner.start(tests);
			if (!r.wasSuccessful()) {
				System.err.println("Tests failed!");
			}
		} catch (Exception e) {
			System.err.println("Tests failed via exception!");
		}
	}
}